package com.jack.payment.service.strategy.impl;


import com.jack.payment.cache.CacheRedisServiceImpl;
import com.jack.payment.constant.SettingRuleConstant;
import com.jack.payment.dao.config.ConfigPayWechatDao;
import com.jack.payment.entity.business.ApplyRefundBo;
import com.jack.payment.entity.business.RequestPayBo;
import com.jack.payment.entity.common.BusinessException;
import com.jack.payment.entity.generate.config.ConfigPayWechatModel;
import com.jack.payment.entity.result.ResultPayVo;
import com.jack.payment.service.strategy.PaymentStrategy;
import com.jack.payment.service.strategy.wechat.CreateWechatPayStrategy;
import com.jack.payment.util.auth.DataAuthUtils;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

@Service("WechatPay")
public class WechatPaymentService implements PaymentStrategy {
    private Logger log = LoggerFactory.getLogger(StripePaymentService.class);

    private volatile Config config;
    private volatile ConfigPayWechatModel payWechatModel;

    private static BigDecimal ONE_HUNDRED = new BigDecimal("100");


    @Autowired
    private Map<String, CreateWechatPayStrategy>  wechatPayStrategyMap;

    @Autowired
    private ConfigPayWechatDao configPayWechatDao;
    @Autowired
    private DataAuthUtils dataAuthUtils;
    @Autowired
    private CacheRedisServiceImpl cacheRedisService;



    @Override
    public synchronized void updatePaymentMethodConfig(Integer id) throws BusinessException {
        payWechatModel = dataAuthUtils.decodeConfigWechat(configPayWechatDao.getAppointConfigPayWechat(id));
        try {
            config = new RSAAutoCertificateConfig.Builder()
                    .merchantId(payWechatModel.getMerchantId())
                    .privateKey(payWechatModel.getProveBook())
                    .merchantSerialNumber(payWechatModel.getMchSerialNo())
                    .apiV3Key(payWechatModel.getApiV3())
                    .build();

        } catch (Exception e) {
            log.error("严重配置错误!!! 这可能导致Wechat支付服务不可用, 错误信息如下" + e);
            e.printStackTrace();
        }
    }

    @Override
    public ResultPayVo createPaymentInfo(RequestPayBo requestPayBo) throws BusinessException {
        String getWechatWebHook = cacheRedisService.getCacheRuleContent(SettingRuleConstant.WECHAT_WEBHOOK);
        ResultPayVo resultPayVo = null;
        try {
           resultPayVo =
                    wechatPayStrategyMap.get(requestPayBo.getPayPlatform())
                            .getWechatPaymentDetail(config, requestPayBo, ONE_HUNDRED, getWechatWebHook, payWechatModel);
        } catch (Exception e) {
            log.warn("wechat 未找到对应平台的支付方式" + e);
            throw new BusinessException();
        }
        return resultPayVo;
    }
}
